<?php
/**
 * Created by PhpStorm.
 * User: zlc
 * Date: 2018-07-19
 * Time: 上午 11:43
 * 会员控制器
 */
namespace app\admin\controller;
use app\admin\model\MemberModel;
use app\admin\model\CarModel;
use app\admin\model\RecodeModel;
class Member extends Base{
    public function index(){
        if(request()->isAjax()){

            $param = input('param.');

            $limit = $param['pageSize'];
            $offset = ($param['pageNumber'] - 1) * $limit;

            $where = [];
            if (!empty($param['phone'])) {
                $where['member_mobile'] = ['like', '%' . $param['phone'] . '%'];
            }
            if (!empty($param['status'])) {
                $where['member_status'] = $param['status'];
            }
            if (!empty($param['name'])) {
                $where['member_name'] = ['like', '%' . $param['name'] . '%'];
            }
            $where['member_isdel'] = 1;
            $member = new MemberModel();
            $selectResult = $member->getMemberByWhere($where, $offset, $limit);
            $car = new CarModel();
            foreach($selectResult as $key=>$vo){

                $selectResult[$key]['member_sex']=checkSex($vo['member_sex']);
                $selectResult[$key]['member_car'] = $car->getCarNum($vo['member_id']);
                $selectResult[$key]['member_status'] = checkMember($vo['member_status']);
                $selectResult[$key]['operate'] = showOperate($this->makeButton($vo['member_id']));
            }

            $return['total'] = $member->getMemberCount($where);  // 总数据
            $return['rows'] = $selectResult;

            return json($return);
        }
        return $this->fetch();
    }
    /**
     * 冻结账户
     */
    public function memberFrozen(){
        $member = new MemberModel();
        $param = input('param.');
        if(!$param['id']){
            ajaxReturn(-1,'冻结失败');
        }
        if(!$param['status']){
            ajaxReturn(-1,'冻结失败');
        }
        $map['member_id'] = $param['id'];
        $data['member_status'] = $param['status'];
        $res = $member->getMemberUpdate($map,$data);
        if($res === false){
            ajaxReturn(-1,'操作失败');
        }else{
            ajaxReturn(1,'操作成功');
        }
    }
    /**
     * 删除会员账号
     */
    public function memberDel(){
        $member = new MemberModel();
        $param = input('param.');
        if(!$param['id']){
            ajaxReturn(-1,'删除失败');
        }
        $map['member_id'] = $param['id'];
        $data['member_isdel'] = 0;
        $res = $member->getMemberUpdate($map,$data);
        if($res === false){
            ajaxReturn(-1,'操作失败');
        }else{
            ajaxReturn(1,'操作成功');
        }
    }
    /**
     * 会员详情
     */
    public function memberDetail(){
        $mid = input('param.id');
        $member = new MemberModel();
        $car = new CarModel();
        $memberInfo =$member->getMemberInfo($mid);
        $memberInfo['member_car'] = $car->getMemberCarList($memberInfo['member_id']);
        $memberInfo['member_sex'] = checkSex($memberInfo['member_sex']);
        $memberInfo['member_status'] = checkMember($memberInfo['member_status']);
        $this->assign('member',$memberInfo);
        return $this->fetch();
    }
    /**
     * 会员签到记录
     */
    public function userSign(){
        if(request()->isAjax()){

            $param = input('param.');

            $limit = $param['pageSize'];
            $offset = ($param['pageNumber'] - 1) * $limit;

            $where = [];
            if (!empty($param['phone'])) {
                $where['snake_member.member_mobile'] = ['like', '%' . $param['phone'] . '%'];
            }
            if (!empty($param['name'])) {
                $where['snake_member.member_name'] = ['like', '%' . $param['name'] . '%'];
            }
            if (!empty($param['type'])) {
                $where['snake_recode.recode_type'] = $param['type'];
            }
            $recode = new RecodeModel();
            $join = "snake_member";
            $condition="snake_recode.member_id = snake_member.member_id";

            $selectResult = $recode->getRecodeByWhere($where, $offset, $limit,$join,$condition);
            foreach($selectResult as $key=>$vo){
                $selectResult[$key]['checked'] = "<input type='checkbox' name='check' value='".$vo['recode_id']."'>";
                $selectResult[$key]['recode_time'] = date('Y-m-d',$vo['recode_time']);
                $selectResult[$key]['recode_type'] = $recode->getType($vo['recode_type']);
            }
            $return['total'] = $recode->getRecodeCount($where,$join,$condition);  // 总数据
            $return['rows'] = $selectResult;

            return json($return);
        }
        return $this->fetch();
    }
    public function recodeDel(){
        $recode = new RecodeModel();
        $param = input('param.');
        if(!$param['id']){
            ajaxReturn(-1,'删除失败');
        }

        $map= " recode_id in (".implode(',',explode('8',$param['id'])).")";
        $res = $recode->getRecodeDel($map);
        if($res === false){
            ajaxReturn(-1,'操作失败');
        }else{
            ajaxReturn(1,'操作成功');
        }
    }
    /**
     * 拼装操作按钮
     * @param $id
     * @return array
     */
    private function makeButton($id)
    {
        return [
            '冻结' => [
                'auth' => 'member/memberfrozen',
                'href' =>  "javascript:memberFrozen(" . $id . ")",
                'btnStyle' => 'primary',
                'icon' => 'fa fa-paste'
            ],
            '解冻' => [
                'auth' => 'member/memberfrozen',
                'href' =>  "javascript:membernoFrozen(" . $id . ")",
                'btnStyle' => 'default',
                'icon' => 'fa fa-paste'
            ],
            '删除' => [
                'auth' => 'member/memberdel',
                'href' => "javascript:memberDel(" . $id . ")",
                'btnStyle' => 'danger',
                'icon' => 'fa fa-trash-o'
            ],
            '查看详情' => [
                'auth' => 'member/memberdetail',
                'href' => url('member/memberDetail', ['id' => $id]),
                'btnStyle' => 'info',
                'icon' => 'fa fa-clipboard-list'
            ]
        ];
    }
}